const { Controller } = require('egg')

/**
 * @controller Region 省市区
 */
class RegionController extends Controller {
	/**
	 * @summary 省
	 * @description 省
	 * @router get /admin/province
	 */
	async province() {
		const ctx = this.ctx
		try {
			const data = await ctx.service.region.province()
			if (data.status === 'success') {
				ctx.helper.success(data.data)
			} else {
				ctx.helper.error(data)
			}
		} catch (e) {
			ctx.helper.error(e)
		}
	}
	/**
	 * @summary 市
	 * @description 市
	 * @router get /admin/province/{id}
	 * @request path string *id 标识
	 */
	async city() {
		const ctx = this.ctx
		try {
			const { id } = ctx.params
			const data = await ctx.service.region.city(id)
			if (data.status === 'success') {
				ctx.helper.success(data.data)
			} else {
				ctx.helper.error(data)
			}
		} catch (e) {
			ctx.helper.error(e)
		}
	}
	/**
	 * @summary 区
	 * @description 区
	 * @router get /admin/area/{id}
	 * @request path string *id 标识
	 */
	async area() {
		const ctx = this.ctx
		try {
			const { id } = ctx.params
			const data = await ctx.service.region.area(id)
			if (data.status === 'success') {
				ctx.helper.success(data.data)
			} else {
				ctx.helper.error(data)
			}
		} catch (e) {
			ctx.helper.error(e)
		}
	}
}

module.exports = RegionController
